package maven.jyhs.com.dao;




import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Objects;
import java.util.Properties;

public abstract class BaseDao {
  public   static String driver;
    public  static String url;
    public static String username;
    public static String password;
    Connection connection;
    PreparedStatement ps;
    static {
        init();
    }
   public static void init(){
       System.out.println("初始化");
       Properties p=new Properties();
     InputStream is= BaseDao.class.getResourceAsStream("/pr.properties");
       System.out.println("asdfasdfsa"+is);
       try {
          p.load(is);
       } catch (IOException e) {
           e.printStackTrace();
       }
       driver=  p.getProperty("driver");
       url=   p.getProperty("url");
       username=  p.getProperty("username");
       password= p.getProperty("password");
   }
   public Connection JNDIConn() throws NamingException, SQLException {
       Context ctx=new InitialContext();
       DataSource ds= (DataSource) ctx.lookup("java:comp/env/jdbc/datasour");
       Connection conn=ds.getConnection();
       return conn;
   }

   public Connection conn(){
       try {
           Class.forName(driver);
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }
       try {
           connection= DriverManager.getConnection(url,username,password);
       } catch (SQLException e) {
           e.printStackTrace();
       }
       return connection;
   }




   public  ResultSet getSelect(String sql, Objects... param){
        conn();
        ResultSet rs=null;
       try {
           ps=connection.prepareStatement(sql);
           for (int i = 0; i < param.length; i++) {
               ps.setObject(i+1,param[i]);
           }
           rs = ps.executeQuery();
       } catch (SQLException e) {
           throw new RuntimeException(e);
       }
       return  rs;
   }

    public int getUpdate(String sql, Objects... param){
        conn();
        int count;
        try {
            ps=connection.prepareStatement(sql);
            for (int i = 0; i < param.length; i++) {
                ps.setObject(i+1,param[i]);
            }
            count = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return  count;
    }

   public void close(Connection connection, PreparedStatement pr, ResultSet ru){
        if(ru!=null){
            try {
                ru.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(pr!=null){
            try {
                pr.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
   }

    public abstract int xuogai(int jq, String skname, String fkname);
}
